package map.coreui.fragment

import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.search.poi.PoiDetailResult
import com.zjw.zy.R
import com.zjw.zy.coreui.view.SlideButton
import com.zjw.zy.ext.go
import com.zjw.zy.ext.longToast
import com.zjw.zy.ext.mapActivity
import map.coreui.view.PoiDetailView
import map.map.MapManager
import map.utils.MapManagerFactory
import map.viewmodel.*

class LoverLocationFragment : MapBaseFragment() {


    private val mapViewModel: MapViewModel by viewModels()
    private val previewViewModel: PreviewViewModel by viewModels()
    private val loverLocationViewModel: LoverLocationViewModel by viewModels()
    private val mapFunctionViewModel: MapFunctionViewModel by activityViewModels()
    private val routePlanViewModel: RoutePlanViewModel by activityViewModels()

    private val locationBtn: SlideButton by lazy { findViewById(R.id.map_lover_location_btn) }
    private val lineBtn: SlideButton by lazy { findViewById(R.id.map_lover_line_btn) }
    private val poiDetailView: PoiDetailView by lazy { findViewById(R.id.map_lover_location_detail) }


    override fun onPageEnter() {
        mapViewModel.toSelfLocation(false)

        mapViewModel.hideZoom()
    }

    override fun onViewRestore(savedInstanceState: Bundle?) {
        mapFunctionViewModel.getLoveLatLng().value?.apply {
            previewViewModel.previewLover(this)

            lineBtn.getEnable().let {
                if (it) {
                    previewViewModel.preLineToMe(this)
                }
            }
        }
    }

    override fun onSubscribe() {
        mapFunctionViewModel.getLoveLatLng().observe(this) {
            previewViewModel.previewLover(it)
        }

        loverLocationViewModel.getLocationInfo().observe(this) { location ->
            poiDetailView.bindData(PoiDetailResult().apply {
                this.address = location.address
                this.name = location.poiList[0].name
                this.uid = location.poiList[0].uid
                this.location = location.location
            })
            poiDetailView.visibility = View.VISIBLE
        }
    }

    override fun onBindViewListener() {

        locationBtn.bindListener(object : SlideButton.Listener {
            override fun onEnable(enable: Boolean) {
                if (enable) {
                    mapFunctionViewModel.getLoveLatLng().value?.let {
                        previewViewModel.preViewSinglePoi(it)
                        loverLocationViewModel.searchLocationInfo(it)
                    }
                } else {
                    mapFunctionViewModel.getLoveLatLng().value?.let {
                        previewViewModel.previewLover(
                            it
                        )
                    }
                    poiDetailView.visibility = View.GONE
                }
            }
        })

        lineBtn.bindListener(object : SlideButton.Listener {
            override fun onEnable(enable: Boolean) {
                if (enable) {
                    mapFunctionViewModel.getLoveLatLng().value?.let {
                        previewViewModel.preLineToMe(it)

                    }
                } else {
                    previewViewModel.removeLineToMe()
                }
            }
        })

        poiDetailView.bindListener(object : PoiDetailView.PoiDetailListener {
            override fun onClickGo(poi: PoiDetailResult) {
                routePlanViewModel.walkPlan(poi.location) {
                    if (it) {
                        //规划成功，跳转规划页面
                        go(R.id.nav_map_multiroute_fragment)
                    } else {
                        "规划失败".longToast()
                    }
                }
            }

            override fun onClickDetails(detailUrl: String) {

            }

            override fun onClickPhone(text: CharSequence?) {

            }
        })
    }

    override fun onBindViewValue() {}
    override fun onFindView(rootView: View) {}
    override fun onLayoutId(): Int {
        return R.layout.lover_location_fragment
    }

    override fun onBackClick() {
        mapActivity().finish()
    }

    override fun onPageExit() {}
}